<template>
  <div class="app-container">
    <el-row type="flex" align="center" :gutter="20">
      <el-form class="filterData-block" label-position="left" :inline="true" label-width="auto">
        <el-form-item label="提交时间:">
          <el-date-picker
            v-model="time"
            value-format="yyyy-MM-dd HH:mm:ss"
            size="small"
            clearable
            type="datetimerange"
            range-separator="至"
            start-placeholder="开始时间"
            end-placeholder="结束时间"
            align="left"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button size="small" icon="el-icon-search" type="primary" @click="refreshTable">搜索</el-button>
        </el-form-item>
        <el-form-item>
          <el-button size="small" icon="el-icon-download" type="primary" @click="exportRecord">导出花名册</el-button>
        </el-form-item>
      </el-form>
    </el-row>
    <el-row style="margin-top: 20px">
      <el-table v-loading="tableConf.loading" :stripe="true" :border="true" :data="tableData">
        <el-table-column align="center" width="50px" prop="index" label="ID"/>
        <el-table-column align="center" min-width="100px" prop="streetName" label="街道"/>
        <el-table-column align="center" min-width="100px" prop="num" label="数量"/>
        <el-table-column align="center" width="200px" label="操作">
          <template slot-scope="{row}">
            <el-button size="mini" icon="el-icon-user" type="primary"  @click="openDetail(row.streetId,0)">花名册
            </el-button>

          </template>
        </el-table-column>
      </el-table>
    </el-row>

    <el-dialog width="70%"  v-if="visible[0]" :visible.sync="visible[0]" title="花名册" :close-on-click-modal="false"
               :close-on-press-escape="false" :destroy-on-close="true"
    >
      <DetailHelp ref="detail" :id="detailId" :time="time" :closeDialog="closeDialog"></DetailHelp>
      <span slot="footer" class="dialog-footer">
        <el-button plain type="primary" @click="visible[0] = false">取 消</el-button>
        <el-button type="primary" class="primary" @click="visible[0] = false">确 定</el-button>
      </span>
    </el-dialog>

  </div>
</template>

<script>

import DetailHelp from '@/components/StreetCode/DetailHelp'

import {createFile} from "@/utils/utils";
import {exportRosterApi, getCustomerByStreetApi} from "@/api/customer";


export default {
  components: { DetailHelp },
  data() {
    return {
      user: {},
      tableData: [],
      nationOption: [],
      educationOption: [],
      groupOption:[],
      townGroupOption:[],
      practiceStateOption: [
        {id: 1,name: "务农"},
        {id: 2,name: "就业"}
      ],
      wishWorkOption: [
        {id: 1,name: "是"},
        {id: 2,name: "否"}
      ],
      householdOption: [
        {id: 1,name: "城镇"},
        {id: 2,name: "农村"}
      ],
      tableConf: {
        page: 1,
        sizes: [10, 20, 50, 100],
        total: 0,
        size: 10,
        loading: false
      },
      visible: {//控制弹窗显示
        0: false,
        1: false,
        2: false
      },
      detailId: null,
      filterData: {
        realName: '',
        startTime: '',
        endTime: '',
        household:[],
        nationId:[],
        educationId: [],
        practiceState:[],
        groups:[],
        wishWork:[],
        page: 0,
        limit: 0
      },
      time:[],
      placeTypeLoading: true,

    }
  },
  mounted() {
    this.getTableList()
  },
  methods: {
    downloadCode(url) {
      window.open(url)
    },

    //打开添加/编辑页
    openDetail(id = null,index) {
      this.detailId = id
      this.visible[index] = true
    },
    //关闭指定弹窗并刷新页面
    closeDialog(index) {
      this.visible[index] = false
      this.getTableList()
    },
    // 每页显示数量change
    handleSizeChange(e) {
      this.tableConf.size = e
      this.getTableList()
    },
    // 当前页change
    handleCurrentChange(e) {
      this.tableConf.page = e
      this.getTableList()
    },
    householdChange()
    {
        this.filterData.groups = []
    },
    //获取列表
    getTableList() {
      this.tableConf.loading = true
      const form=JSON.parse(JSON.stringify(this.filterData));
      if(this.time!=null)
      {
        form.startTime = this.time[0];
        form.endTime = this.time[1];
      }
      getCustomerByStreetApi(form).then((res) => {
        this.tableData = res
      }).finally(() => {
        this.tableConf.loading = false
      })
    },
    //导出当前条件excel
    exportRecord(){
      const form=JSON.parse(JSON.stringify(this.filterData));
      if(this.time!=null)
      {
        form.startTime = this.time[0];
        form.endTime = this.time[1];
      }
      this.$confirm('确定导出当前查询条件的数据吗','提示').then(()=>{
        const loading=this.$loading({ text: '处理中，请勿关闭页面' })
        exportRosterApi(form).then((res)=>{
          if(!createFile(res,'花名册')){
            this.$message.error('未知的文件格式');
          }
        }).finally(()=>{
          loading.close();
        })
      })
    },


    refreshTable() {
      this.tableConf.page = 1
      this.getTableList()
    }
  }
}
</script>
<style lang="scss" scoped>
.filterData-block {
  .el-form-item {
    margin-bottom: 0;
  }
}
</style>
